package com.study.common.aspect;

import com.study.common.enumerate.Log4jInfo;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author 谭永超 [www.tanyongchao.tk:8090/SumDome/index]
 * @Date 2018/9/10 10:12
 * @Description
 */
@Aspect
@Component
public class HttpAspect {
    private static final Log log = LogFactory.getLog(HttpAspect.class);

    @Pointcut("execution(public * com.study.main.controller.MainController.toShow(..))")
    public void log() {

    }

    // before 在方法执行之前执行
    @Before("log()")
    public void doBefore(JoinPoint jinPoint) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        //url
        log.info(Log4jInfo.HEAD+"url:" + request.getRequestURL()
                + " 方法:" + request.getMethod()
                + " ip:" + request.getRemoteAddr()
                + " 类方法:" + jinPoint.getSignature().getDeclaringType() + "." + jinPoint.getSignature().getDeclaringTypeName()
                + " 参数:" + jinPoint.getArgs());
    }

    // after 在方法执行之后执行
    @After("log()")
    public void doAfter() {
        System.out.println("a");
    }

}
